package top.ztf.erp.utils;



import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 数据校验工具类
 */
public class BeanValidatorUtils {

    private BeanValidatorUtils(){}

    private static final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

    public static Map<String,String> validate(Object object, Class addClass){
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<Object>> violationSet = validator.validate(object,addClass);
        Map<String, String> emptyMap = new HashMap<>();
        for (ConstraintViolation<Object> violation : violationSet) {
            emptyMap.put(violation.getPropertyPath().toString(),violation.getMessage());
        }
        return emptyMap;
    }
}
